package dao;

import java.util.ArrayList;
import java.util.List;
import model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class UserMapperTest
{
  @Autowired
  private UserMapper userMapper;
  
  public UserMapper getUserMapper()
  {
    return this.userMapper;
  }
  
  public void setUserMapper(UserMapper userMapper) {
    this.userMapper = userMapper;
  }
  
  @Before
  public void setUp() throws Exception {
    ApplicationContext context = new FileSystemXmlApplicationContext(
      new String[] { "src/main/resources/spring-mvc.xml", "src/main/resources/spring.xml", "src/main/resources/spring-mybatis.xml" });
    this.userMapper = ((UserMapper)context.getBean("userMapper"));
  }
  
  @After
  public void tearDown() throws Exception
  {}
  
  @Test
  public void test_insert() {
    User u = new User();
    u.setUsername("user1");
    u.setLoginPwd("user1");
    u.setGender("1");
    this.userMapper.insert(u);
  }
  
  @Test
  public void test_edit() {
    User user = this.userMapper.selectUserById("4");
    System.out.println("user " + user);
    user.setLoginPwd("888888");
    this.userMapper.update(user);
  }
  
  @Test
  public void test_batchInsert() {
    List<User> list = new ArrayList();
    for (int i = 0; i < 10; i++) {
      User user = new User();
      user.setUsername("user" + i);
      user.setLoginPwd("user" + i);
      user.setGender("1");
      list.add(user);
    }
    this.userMapper.saveUserList(list);
  }
  
  @Test
  public void test_del() {
    this.userMapper.del("4", "1");
  }
}

